<?php

namespace backend\controllers;

use common\services\UserService;
use Yii;
use common\models\LydbUser;
use common\models\LydbUserQuery;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * LydbUserController implements the CRUD actions for LydbUser model.
 */
class LydbUserController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all LydbUser models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new LydbUserQuery();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }


    public function actionGenerate()
    {
        $startTime = time();
        ini_set('max_execution_time',300);


        
        if(Yii::$app->request->post()) {
            $maxNumber = Yii::$app->request->post('maxNumber');
            $maxNumber = $maxNumber ? $maxNumber : 100;
            $successNumber = (new UserService())->userMultiCreate($maxNumber);

            $costTime  = time() - $startTime;
            $data = [
                'maxNumber' =>  $maxNumber
            ];

            return $this->render('generate',['result'=>$data]);
        }else{
            return $this->render('generate');
        }
    }

    /**
     * Finds the LydbUser model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return LydbUser the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = LydbUser::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
